import request from '../utils/request';

/**
 * 分类相关API
 */
const categoryApi = {
  /**
   * 获取分类列表
   * @returns {Promise} - 分类列表结果
   */
  getCategories: async () => {
    try {
      const response = await request.get('/admin/categories');
      return response;
    } catch (error) {
      console.error('获取分类列表失败:', error);
      throw error;
    }
  },
  
  /**
   * 添加分类
   * @param {Object} category - 分类数据
   * @param {string} category.name - 分类名称
   * @returns {Promise} - 添加结果
   */
  addCategory: async (category) => {
    try {
      const response = await request.post('/admin/categories', category);
      return response;
    } catch (error) {
      console.error('添加分类失败:', error);
      throw error;
    }
  },
  
  /**
   * 更新分类
   * @param {number|string} id - 分类ID
   * @param {Object} category - 分类数据
   * @returns {Promise} - 更新结果
   */
  updateCategory: async (id, category) => {
    try {
      const response = await request.put(`/admin/categories/${id}`, category);
      return response;
    } catch (error) {
      console.error('更新分类失败:', error);
      throw error;
    }
  },
  
  /**
   * 删除分类
   * @param {number|string} id - 分类ID
   * @returns {Promise} - 删除结果
   */
  deleteCategory: async (id) => {
    try {
      const response = await request.delete(`/admin/categories/${id}`);
      return response;
    } catch (error) {
      console.error('删除分类失败:', error);
      throw error;
    }
  },
  

};

export default categoryApi;
export { categoryApi };
